using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Ink;

using System.Windows;
using System.Diagnostics;
using WozLib.UI.Ink.SketchObjects;

namespace WozLib.UI.Ink.SketchCommands
{
  public class EraseSketchCommand : SketchCommand
  {

    public EraseSketchCommand()
    {
    }

    public EraseSketchCommand(SketchObject erasedWozObject)
      : base(erasedWozObject)
    {
    }

    public EraseSketchCommand(IEnumerable<SketchObject> erasedWozObjects)
      : base(erasedWozObjects)
    {
    }

    public override void Redo()
    {
      if (this.TargetWozObjects != null && this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        StrokeCollection strokes = new StrokeCollection();
        List<UIElement> uies = new List<UIElement>();
        foreach (SketchObject wozObject in this.TargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {
            if (iWozObject is Stroke)
            {
              strokes.Add(iWozObject as Stroke);
            }
            else if (iWozObject is UIElement)
            {
              uies.Add(iWozObject as UIElement);
            }
          }
          else
          {
            if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas != null)
            {
              if (this.WozClientCommandReferences.InkOverlayCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkOverlayCanvas.Children.Remove(iWozObject as UIElement);
              }
            }
            else if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas == null)
            {
              if (this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                uies.Add(iWozObject as UIElement);
              }
            }
          }
        }

        // to make sure this doesn't die we could check to make sure these strokes and uies are infact in the canvas
        try
        {
          this.WozClientCommandReferences.InkCanvas.Strokes.Remove(strokes);
          foreach (UIElement uie in uies)
          {
            this.WozClientCommandReferences.InkCanvas.Children.Remove(uie);
          }
        }
        catch (Exception ex)
        {
          Debug.WriteLine(ex.ToString());
        }
      }
    }

    public override void Undo()
    {
      if (this.TargetWozObjects != null && this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        StrokeCollection strokes = new StrokeCollection();
        List<UIElement> uies = new List<UIElement>();
        foreach (SketchObject wozObject in this.TargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {
            if (iWozObject is Stroke)
            {
              strokes.Add(iWozObject as Stroke);
            }
            else if (iWozObject is UIElement)
            {
              uies.Add(iWozObject as UIElement);
            }
          }
          else
          {
            if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas != null)
            {
              if (!this.WozClientCommandReferences.InkOverlayCanvas.Children.Contains(iWozObject as UIElement))
              {
                this.WozClientCommandReferences.InkOverlayCanvas.Children.Add(iWozObject as UIElement);
              }
            }
            else if (iWozObject is UIElement && this.WozClientCommandReferences.InkOverlayCanvas == null)
            {
              if (!this.WozClientCommandReferences.InkCanvas.Children.Contains(iWozObject as UIElement))
              {
                uies.Add(iWozObject as UIElement);
              }
            }
          }
        }

        // to make sure this doesn't die we could check to make sure these strokes and uies are infact NOT in the canvas
        try
        {
          this.WozClientCommandReferences.InkCanvas.Strokes.Add(strokes);
          foreach (UIElement uie in uies)
          {
            this.WozClientCommandReferences.InkCanvas.Children.Add(uie);
          }
        }
        catch (Exception ex)
        {
          Debug.WriteLine(ex.ToString());
        }
      }
    }
  }
}
